﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SISCON.Dao;
using SISCON.Model;
using SISCON.Common;
using log4net;
using SISCON.Vo;

namespace SISCON.Bl
{
    public class TeacherBL
    {
        private static ILog _Logger = LogManager.GetLogger(typeof(UserBL));
        private SessionBL _SessionBL = new SessionBL();
        private TeacherDAO _TeacherDao = new TeacherDAO();
        private UserBL _UserBL = new UserBL();

        /// <summary>
        /// Valida los usuarios que tienen acceso al metodo GetTeacher y manda a llamar al Dao correspondiente
        /// </summary>
        /// <param name="sessionUUID">Sesion del sistema</param>
        /// <param name="teacherId">Id interno del maestro</param>
        /// <returns>Maestro</returns>
        public Teacher GetTeacher(string sessionUUID, long teacherId)
        {
            try
            {
                _UserBL.RequireRole(sessionUUID, Role.READ_ACCESS, Role.TEACHERS);
                return _TeacherDao.GetTeacher(teacherId);
            }
            catch (Exception ex)
            {
                if (!(ex is SISCONException))
                {
                    _Logger.Error("Error in GetTeachers()", ex);
                }
                throw ex;
            }
        }

        /// <summary>
        /// Valida los usuarios que tienen acceso al metodo GetTeacher y manda a llamar al Dao correspondiente
        /// </summary>
        /// <param name="sessionUUID">Sesion del sistema</param>
        /// <param name="schoolId">Nomina del maestro</param>
        /// <returns>Maestro</returns>
        public Teacher GetTeacher(string sessionUUID, string schoolId)
        {
            try
            {
                _UserBL.RequireRole(sessionUUID, Role.READ_ACCESS, Role.TEACHERS);
                return _TeacherDao.GetTeacher(schoolId);
            }
            catch (Exception ex)
            {
                if (!(ex is SISCONException))
                {
                    _Logger.Error("Error in GetTeachers()", ex);
                }
                throw ex;
            }
        }

        /// <summary>
        /// Valida los usuarios que tienen acceso al metodo GetTeacherByName y manda a llamar al Dao correspondiente
        /// </summary>
        /// <param name="sessionUUID">Sesion del sistema</param>
        /// <param name="teacherName">Nombre del maestro</param>
        /// <returns>Lista de cursos</returns>
        public List<Course> GetTeacherByName(string sessionUUID, string teacherName)
        {
            try
            {
                long userId = _UserBL.RequireRole(sessionUUID, Role.READ_ACCESS, Role.TEACHERS);
                return _TeacherDao.GetTeacherByName(teacherName, userId);
            }
            catch (Exception ex)
            {
                if (!(ex is SISCONException))
                {
                    _Logger.Error("Error in GetTeachers()", ex);
                }
                throw ex;
            }
        }

        /// <summary>
        /// Valida los usuarios que tienen acceso al metodo GetAllTeachers y manda a llamar al Dao correspondiente
        /// </summary>
        /// <param name="sessionUUID">Sesion del sistema</param>
        /// <returns>Lista de maestros</returns>
        public List<Teacher> GetAllTeachers(string sessionUUID)
        {
            List<Teacher> teacherList = new List<Teacher>();

            try
            {
                _UserBL.RequireRole(sessionUUID, Role.READ_ACCESS, Role.TEACHERS);
               return _TeacherDao.GetAllTeachers();
            }
            catch (Exception ex)
            {
                if (!(ex is SISCONException))
                {
                    _Logger.Error("Error in GetAllTeachers()", ex);
                }
                throw ex;
            }
        }

        /// <summary>
        /// Valida los usuarios que tienen acceso al metodo DeleteTeacher y manda a llamar al Dao correspondiente
        /// </summary>
        /// <param name="sessionUUID">Sesion del sistema</param>
        /// <param name="teacherId">Id interno del maestro</param>
        public void DeleteTeacher(string sessionUUID, string teacherId)
        {
            try
            {
                _UserBL.RequireRole(sessionUUID, Role.WRITE_ACCESS, Role.TEACHERS);
                _TeacherDao.DeleteTeacher(teacherId);
            }
            catch (Exception ex)
            {
                if (!(ex is SISCONException))
                {
                    _Logger.Error("Error in DeleteTeacher()", ex);
                }
                throw ex;
            }

        }

        /// <summary>
        /// Valida los usuarios que tienen acceso al metodo SaveTeacher y manda a llamar al Dao correspondiente
        /// </summary>
        /// <param name="sessionUUID">Sesion del sistema</param>
        /// <param name="teacherId">Id interno del maestro</param>
        /// <param name="name">Nombre del maestro</param>
        /// <param name="schoolId">Nomina del maestro</param>
        public void SaveTeacher(string sessionUUID, string teacherId, string name, string schoolId, string userId)
        {
            try
            {
                _UserBL.RequireRole(sessionUUID, Role.WRITE_ACCESS, Role.TEACHERS);
                _TeacherDao.SaveTeacher(teacherId, name, schoolId, long.Parse(userId));
            }
            catch (Exception ex)
            {
                if (!(ex is SISCONException))
                {
                    _Logger.Error("Error in SaveTeacher()", ex);
                }
                throw ex;
                
            }
        }

        /// <summary>
        /// Valida los usuarios que tienen acceso al metodo GetTeacherBySchoolId y manda a llamar al Dao correspondiente
        /// </summary>
        /// <param name="sessionUUID">Sesion del sistema</param>
        /// <param name="schoolId">Nomina del maestro</param>
        /// <returns>Lista de cursos</returns>
        public List<Course> GetTeacherBySchoolId(string sessionUUID, string schoolId)
        {
            try
            {
                long userId =_UserBL.RequireRole(sessionUUID, Role.READ_ACCESS, Role.TEACHERS);
                return _TeacherDao.GetTeacherBySchoolId(schoolId, userId);
            }
            catch (Exception ex)
            {
                if (!(ex is SISCONException))
                {
                    _Logger.Error("Error in GetTeacherBySchoolId()", ex);
                }
                throw ex;
            }
        }

    }
}